#include "DashBehaviour.h"

#include "Player/World/Ball.h"

/*
 * Vykona prikaz dash a navyse sa pozrie za loptou
 *
 * @param dashPower velkost zrychlenia pri prikaze dash
 */
void DashBehaviour::Behave(float dashPower)
{
	Ball tmpBall(*world->ball); 
	tmpBall.Step();
	commandGenerator->Dash(dashPower);
	GetTurnNeckToBehaviour()->Behave(tmpBall.pos, world->me->pos + 
		GetRunToPositionBehaviour()->PredictNextVelocity(dashPower));
}

/*
 * vypocita najlepsiu hodnotu zrychlenia pre prikaz dash
 * 
 * @param pointFuture bod, kam sa chceme dostat
 * @param me referencia hraca
 * @param bestPoint vypocitany bod, kam sa dashom dostaneme
 * @param step krok, ktorym sa bude pocitat velkost zrychlenia
 *
 * @return hodnota najlepsieho zrychlenia na to aby sme sa dostali co najblizsie do daneho bodu
 */
float DashBehaviour::GetBestDash(const Point& pointFuture, const Myself& me, Point &bestPoint, float step)
{
	float best_Power = NO_BEST_DASH;
	float best_minDistance = NO_BEST_DASH;
	for(float f = world->params->server->minpower; f <= world->params->server->maxpower; f += step)
	{
		Myself ja(me);
		Myself newJa = GetRunToPositionBehaviour()->AfterDash(ja, f);
		float distance = (pointFuture - newJa.pos).GetLength();
		if(distance < best_minDistance)
		{
			best_minDistance = distance;
			best_Power = f;
			bestPoint = newJa.pos;
		}
	}
	return best_Power;
}
